package com.easycarebn.main.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.ItemDr;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.SuggestNameType;
import com.easycarebn.request.data.DoctorInfo;
import com.easycarebn.request.data.DoctorInfo.Booking;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class SearchResultAdapter extends BaseAdapter {
    ViewHolder holder;
    Context mContext;
    List<ItemDr> arr;
    public IResult callIResult;
    private int mSearchType;

    public SearchResultAdapter(Context mContext, List<ItemDr> arr, IResult caIResult) {
        this.mContext = mContext;
        this.callIResult = caIResult;
        this.arr = arr;
    }

    public void setSearchType(int searchType) {
        mSearchType = searchType;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arr.size();
    }

    @Override
    public ItemDr getItem(int position) {
        return arr.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public class ViewHolder {
        public TextView drname;
        public TextView lblChuyenkhoa;
        public TextView lblDesc;
        public TextView roomName;
        public TextView address;
        public TextView rateDesc;

        public TextView tvBookingNearDate;

        public ImageView avatar;
        public Button btnDatkham;
        public Button btnDatlichtruoc;

        public RatingBar ratingBar;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // Utilities.log("convert null " + position);
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_drtimkiem, null);
            holder.drname = (TextView) convertView.findViewById(R.id.drname);
            holder.lblChuyenkhoa = (TextView) convertView.findViewById(R.id.lblChuyenkhoaDr);
            holder.rateDesc = (TextView) convertView.findViewById(R.id.rateDesc);

            holder.lblDesc = (TextView) convertView.findViewById(R.id.lblDesc);
            holder.roomName = (TextView) convertView.findViewById(R.id.roomName);
            holder.address = (TextView) convertView.findViewById(R.id.address);

            holder.avatar = (ImageView) convertView.findViewById(R.id.dr_avatar);
            holder.btnDatkham = (Button) convertView.findViewById(R.id.btnDatkham);
            holder.btnDatlichtruoc = (Button) convertView.findViewById(R.id.btnDatlichtruoc);

            // holder.lineBook = (LinearLayout) convertView
            // .findViewById(R.id.lineBookKham);
            // holder.lineMeta = (LinearLayout) convertView
            // .findViewById(R.id.lineMeta);
            holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rtbProductRating);
            holder.tvBookingNearDate = (TextView) convertView.findViewById(R.id.tv_booking_neardate);

            convertView.setTag(holder);
        } else {
            // Utilities.log("convert not null " + position);
            holder = (ViewHolder) convertView.getTag();
        }
        ItemDr itemDr = getItem(position);
        final DoctorInfo info = itemDr.getInfo();
        Booking booking = info.getBooking();

        if (booking == null || TextUtils.isEmpty(booking.getNeardate())) {
            holder.btnDatkham.setVisibility(View.GONE);
            holder.btnDatlichtruoc.setVisibility(View.VISIBLE);
        } else {
            holder.btnDatkham.setVisibility(View.VISIBLE);
            holder.btnDatlichtruoc.setVisibility(View.GONE);
        }

        //
        // if (itemDr.isShowbook()) {
        // holder.lineMeta.setVisibility(View.GONE);
        // holder.lineBook.setVisibility(View.VISIBLE);
        //
        // } else {
        // holder.lineMeta.setVisibility(View.VISIBLE);
        // holder.lineBook.setVisibility(View.GONE);
        // }
        holder.drname.setText(info.getName());
        if (mSearchType == SuggestNameType.DOCTOR.getCode() && info.getSpeciality() != null && !info.getSpeciality().isEmpty()) {
            holder.lblChuyenkhoa.setText(String.format(mContext.getResources().getString(R.string.chuyen_khoa_bs), info
                    .getSpeciality().get(0)));
        } else if (mSearchType == SuggestNameType.CLINIC.getCode() && info.getClinictype() != null
                && !info.getClinictype().isEmpty()) {
            holder.lblChuyenkhoa.setText(info.getClinictype().get(0));

        } else if (mSearchType == SuggestNameType.HOME_SERVICE.getCode() && info.getServicetype() != null
                && !info.getServicetype().isEmpty() && !info.getServicetype().get(0).isEmpty()) {
            holder.lblChuyenkhoa.setText(info.getServicetype().get(0));
        } else {
            holder.lblChuyenkhoa.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(info.getPosition())) {
            holder.lblDesc.setVisibility(View.VISIBLE);
            holder.lblDesc.setText(Html.fromHtml(info.getPosition()));
        } else {
            holder.lblDesc.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(info.getClinicname())) {
            holder.roomName.setVisibility(View.VISIBLE);
            holder.roomName.setText(Html.fromHtml(info.getClinicname()));
        } else {
            holder.roomName.setVisibility(View.GONE);

        }
        if (mSearchType == SuggestNameType.DOCTOR.getCode() && !TextUtils.isEmpty(info.getAddressDefault())) {
            holder.address.setText(Html.fromHtml(info.getAddressDefault()));

        } else {
            if (info.getAddress() == null || info.getAddress().length() == 0) {
                /*
                 * Utils.setTextEmpty(holder.address, (Activity) mContext,
                 * mContext.getResources().getString(R.string.dia_chi));
                 */
                holder.address.setText(Html.fromHtml(info.getAddressDefault()));
            } else {
                holder.address.setText(Html.fromHtml(info.getAddress()));
            }
        }
        if (info.getRating() == 0) {
            holder.rateDesc.setVisibility(View.VISIBLE);
            holder.ratingBar.setVisibility(View.GONE);
        } else {
            holder.ratingBar.setVisibility(View.VISIBLE);
            holder.rateDesc.setVisibility(View.GONE);
            holder.ratingBar.setRating(info.getRating());
        }
        // holder.avatar.setImageUrl(info.getAvatar(),
        // Utils.getImageLoader(mContext));
        Log.d("url_avata",info.getAvatar() );
        Picasso.with(mContext).load(info.getAvatar()).resize(Utils.IMG_WIDHT, Utils.IMG_HEIGHT).placeholder(R.drawable.ic_bacsi).error(R.drawable.ic_bacsi).into(holder.avatar);

        if (info.getBooking() != null && !TextUtils.isEmpty(info.getBooking().getNeardate())) {
        	Log.d("info.getBooking().getNeardate()",info.getBooking().getNeardate());
           // String date = DateFormat.format("EEEE, dd-MM-yyyy", Utils.convertDate(info.getBooking().getNeardate(), "yyyy-MM-dd"))
              //      .toString();
            holder.tvBookingNearDate.setText(Html.fromHtml(mContext.getString(R.string.lich_kham_gan_nhat_ps,
                    Utils.setTextcolorHtml("<br>" + info.getBooking().getShowneardate(), "#00a3e0"))));
            // holder.tvBookingNearDate.setEnabled(false);

        } else if (info.getBooking() != null && TextUtils.isEmpty(info.getBooking().getNeardate())) {
            String phone = info.getBooking().getPhonesupport().toString();
            if (phone.isEmpty())
                phone = Utils.phone;

            holder.tvBookingNearDate.setText(Html.fromHtml(mContext.getString(R.string.so_dien_thoai_ho_tro_ps,
                    Utils.setTextcolorHtml("<br>" + phone, "#00a3e0"))));
            // holder.tvBookingNearDate.setCompoundDrawables(mContext.getResources().getDrawable(R.drawable.ic_phone),
            // null, null, null);
        }

        holder.tvBookingNearDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (info.getBooking() != null && TextUtils.isEmpty(info.getBooking().getNeardate())) {
                    String phone = info.getBooking().getPhonesupport().toString();
                    if (phone.isEmpty())
                        phone = Utils.phone;
                    Utils.callPhone(phone, (Activity) mContext);
                }

            }
        });

        holder.btnDatkham.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                callIResult.onDisplay(position, "");
            }
        });

        holder.btnDatlichtruoc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.showDatlichtruoc(mContext, info);
            }
        });

        return convertView;
    }

}
